package com.kun.portal.webframe.controller;

import com.kun.portal.webframe.po.AppointAndSignIn;
import com.kun.portal.webframe.po.AppointmentPo;
import com.kun.portal.webframe.po.SignInPo;
import com.kun.portal.webframe.service.AppointmentService;
import com.kun.portal.webframe.service.RoomService;
import com.kun.portal.webframe.service.SignInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import java.util.Date;

/**
 * Created by Administrator on 2018/5/16.
 */
@Controller
@RequestMapping({
        "/api/signIn"
})

public class SignInController {
    @Autowired
    private SignInService signInService;

    @Autowired
    private AppointmentService appointmentService;

    @Autowired
    private  RoomService roomService;
    @RequestMapping(
            value={"/save"},
            method = RequestMethod.POST,
            produces = {"application/json"}
    )
    @ResponseBody
    @Transactional
    public SignInPo saveSignInPo(@RequestBody AppointAndSignIn appointAndSignIn){
           AppointmentPo appointmentPo=appointAndSignIn.getAppointmentPo();

           SignInPo signInPo= signInService.save(appointAndSignIn.getSignInPo());

           if(appointAndSignIn.getSignInPo().getRoom()!=null){
               roomService.saveRoom(appointAndSignIn.getSignInPo().getRoom());
           }
           if(appointmentPo!=null){
               appointmentPo.setSignIn(signInPo);
               appointmentService.editAppointment(appointmentPo);
           }
           return signInPo;
    }
    @RequestMapping(
            value={"/query"},
            method = RequestMethod.GET,
            produces = {"application/json"}
    )
    @ResponseBody
    public Page<SignInPo> query(@RequestParam(required = false) Pageable pageable , Date startDate, Date endDate, String flightNum){
        return signInService.query(pageable,startDate,endDate,flightNum);
    }
    @RequestMapping(
            value={"/queryService"},
            method = RequestMethod.GET,
            produces = {"application/json"}
    )
    @ResponseBody
    public Page<SignInPo> queryService(@RequestParam(required = false) Pageable pageable ,String roomId, String flightInfoId){
        return signInService.queryService(pageable,roomId,flightInfoId);
    }
 }
